package com.boco.pciv.service.builder.impl;

import cn.hutool.core.util.StrUtil;
import com.boco.pciv.SysProperties;
import com.boco.pciv.core.entity.PageResultBase;
import com.boco.pciv.core.entity.SearchCriteria;
import com.boco.pciv.core.service.PcivBaseServiceImpl;
import com.boco.pciv.core.util.SearchUtil;
import com.boco.pciv.entity.builder.BuilderInfo;
import com.boco.pciv.entity.builder.ProComBuildInfo;
import com.boco.pciv.entity.builder.ProComRes;
import com.boco.pciv.entity.project.CompanyInfo;
import com.boco.pciv.entity.project.ProjectInfo;
import com.boco.pciv.entity.sys.UserInfo;
import com.boco.pciv.mapper.builder.IBuilderMapper;
import com.boco.pciv.service.builder.IBuilderService;
import com.boco.pciv.service.sys.IUserService;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class BuilderServiceImpl extends PcivBaseServiceImpl<IBuilderMapper,BuilderInfo,Integer> implements IBuilderService {

    @Autowired
    private IUserService iUserService;
    @Autowired
    SysProperties sysConfig;

    public String reColoms(BuilderInfo info) {
        Map<String, Object> map = new HashMap();
        map.put("name", info.getBuilderName());
        map.put("licNo", info.getLicno());
        map.put("phone", info.getPhone());
        List<BuilderInfo> list = mapper.queryReList(map);
        String str = "";
        for (BuilderInfo l : list) {
            if (l.getBuilderName().equals(info.getBuilderName())) {
                str += "系统存在重复的"+sysConfig.getBuilderName()+"名称；";
            }
            if (l.getLicno().equals(info.getLicno())) {
                str += "系统存在重复的"+sysConfig.getBuilderName()+"营业执照号；";
            }
            if (l.getPhone().equals(info.getPhone())) {
                str += "系统存在重复的"+sysConfig.getBuilderName()+"责任人手机号；";
            }
        }
        return str;
    }
    public void addUser(BuilderInfo info) {
        UserInfo user = new UserInfo();
        user.setUserId(info.getPhone());
        user.setPhone(info.getPhone());
        user.setUserName(info.getUserName());
        user.setUserType(2);
        user.setDeptId(info.getLicno());
        user.setPassword(info.getDefPwd());
        user.setCreateUser(info.getCreateUser());
        iUserService.addOther(user);

        //添加对应的角色权限
        Map<String,String> reMap = new HashMap<String, String>();
        reMap.put("userId",info.getPhone());
        reMap.put("roleId",info.getBuilderid());
        iUserService.insertUserRole(reMap);
    }
    public void updateUserStatus(Map<String, Object> map){
        mapper.updateUserStatus(map);
    }
    public void deleteBuilder(Map<String, Object> map){
        mapper.deleteBuilder(map);
    }

    public void addProComRes(ProComRes info){
        String[] comids=info.getCompany_id().split(",");
        for(String cid:comids){
            info.setCompany_id(cid);
            String count=mapper.getProComResCount(info);
            if(StrUtil.isBlank(count)||"0".equals(count)){
                mapper.insertProComRes(info);
            }
        }
    }
    public PageResultBase geProBulidComList(SearchCriteria param){
        List<ProComBuildInfo> list= mapper.geProBulidComList(param.getCondMap());
        if(param.getQueryMap().get("UserType")!=null) {
            for (ProComBuildInfo info : list) {
                info.setUserType(param.getQueryMap().get("UserType").toString());
            }
        }
        return SearchUtil.parse(list);
    }
    public String getprojectinfo(String proid){
        List<String> list= mapper.getprojectinfo(proid);
        return list.stream().collect(Collectors.joining(","));
    }
}
